package com.basker.pisces.tools.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.basker.pisces.domain.enums.EnumTypeManager;
import com.basker.pisces.domain.value.meta.EnumType;
import com.basker.pisces.service.annotation.IgnoreLoginContext;
import com.basker.pisces.service.dto.DefaultServiceResult;
import com.basker.pisces.service.dto.ServiceResultFactory;

@RestController
@RequestMapping("/t/enumType")
@ConditionalOnClass(EnumTypeManager.class)
public class EnumTypeRestController {

    @Autowired
    private EnumTypeManager enumTypeManager;

    @GetMapping(value = "/all")
    @IgnoreLoginContext
    public DefaultServiceResult<List<EnumType>> all() {
        List<EnumType> allEnumTypes = this.enumTypeManager.getAllEnumTypes();
        return ServiceResultFactory.ok(allEnumTypes);
    }

    @GetMapping(value = "/{enumType}")
    @IgnoreLoginContext
    public DefaultServiceResult<EnumType> enumType(@PathVariable("enumType") String name) {
        EnumType enumType = this.enumTypeManager.getEnumType(name);
        return enumType != null ? ServiceResultFactory.ok(enumType)
                : ServiceResultFactory.error("enumType '" + name + "' not found!");
    }

}
